package com.xyzh05.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.service.LoginLogManager;
import com.xyzh05.model.LoginLog;
import com.xyzh05.webapp.action.BaseAction;

import java.util.List;

public class LoginLogAction extends BaseAction implements Preparable {
    private LoginLogManager loginLogManager;
    private List loginLogs;
    private LoginLog loginLog;
    private Long id;
    private String query;

    public void setLoginLogManager(LoginLogManager loginLogManager) {
        this.loginLogManager = loginLogManager;
    }

    public List getLoginLogs() {
        return loginLogs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String loginLogId = getRequest().getParameter("loginLog.id");
            if (loginLogId != null && !loginLogId.equals("")) {
                loginLog = loginLogManager.get(new Long(loginLogId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        loginLogs = loginLogManager.search(query, LoginLog.class);
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LoginLog getLoginLog() {
        return loginLog;
    }

    public void setLoginLog(LoginLog loginLog) {
        this.loginLog = loginLog;
    }

    public String delete() {
        loginLogManager.remove(loginLog.getId());
        saveMessage(getText("loginLog.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            loginLog = loginLogManager.get(id);
        } else {
            loginLog = new LoginLog();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (loginLog.getId() == null);

        loginLogManager.save(loginLog);

        String key = (isNew) ? "loginLog.added" : "loginLog.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}